// index.js
// 获取应用实例
const app = getApp()
var r = require('../../utils/request.js')
Page({
  data: {
    itemsImg: [],
    list1: [],
    list2: []
  },

  onLoad() {
    // 修改顶部导航颜色
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#CB1C1D'
    })

  },

  // 登录
  getMsg() {
    wx.showModal({
      title: '未登录',
      content: '是否前往登录！',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.reLaunch({
            url: '/pages/home/pages/login/login',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 底部页面跳转
  skip(e) {
    if (!wx.getStorageSync('token')) {
      this.getMsg()
      return
    }
    let num = e.currentTarget.dataset.num
    if (num == 2) {
      wx.reLaunch({
        url: '/pages/classroom/classroom',
      })
    } else if (num == 3) {
      wx.reLaunch({
        url: '/pages/understand/understand',
      })
    } else if (num == 4) {
      wx.reLaunch({
        url: '/pages/my/my',
      })
    }
  },

  onShow() {
    var that = this
    // 获取轮播图
    r.toGet('/common/bannerPic', {}, (res) => {
      // console.log(res.retData)
      that.setData({
        itemsImg: res.retData
      })
    }) 

    // 获取党务文章 和 党建发布
    r.toGet('/blackOpen/affairsList', {
      userId: wx.getStorageSync('userId'),
      page: 1,
      limit: 2
    }, (res) => {
      // console.log(res.retData.records)
      that.setData({
        list1: res.retData.records
      })
    })

    r.toGet('/blackOpen/balckList', {
      userId: wx.getStorageSync('userId'),
      page: 1,
      limit: 2
    }, (res) => {
      // console.log(res.retData.records)
      that.setData({
        list2: res.retData.records
      })
    })

    if (wx.getStorageSync('token')) {
      r.toGet('/login/getUserInfo', {}, (res) => {
        // console.log(res.retData)
        if (res.retData.isFirst == 1) {
          // 判断是否是第一次登陆，是的话弹窗
          wx.showModal({
            title: ' ',
            content: '初始密码是手机号后六位',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                r.toPost('/login/firstLogin', {}, (res) => {
                  console.log('状态修改成功!')
                })
              }
            }
          })
        }
      })
    }
  },


  // 党+++6+++
  getInto(e) {
    if (!wx.getStorageSync('token')) {
      this.getMsg()
      return
    }
    let num = e.currentTarget.dataset.num
    if (num == 1) {
      wx.navigateTo({
        url: '/pages/home/pages/party1/party1',
      })
    } else if (num == 2) {
      wx.navigateTo({
        url: '/pages/home/pages/party2/party2',
      })
    } else if (num == 3) {
      wx.navigateTo({
        url: '/pages/home/pages/party3/party3',
      })
    } else if (num == 4) {
      wx.navigateTo({
        url: '/pages/home/pages/party4/party4',
      })
    } else if (num == 5) {
      wx.navigateTo({
        url: '/pages/home/pages/party5/party5',
      })
    } else if (num == 6) {
      wx.navigateTo({
        url: '/pages/home/pages/party6/party6',
      })
    } else if (num == 7) { //党务文章
      wx.navigateTo({
        url: '/pages/home/pages/article/article',
      })
    } else if (num == 8) { //党建发布
      wx.navigateTo({
        url: '/pages/home/pages/blackboard/blackboard',
      })
    } else if (num == 9) { //民主评议
      wx.navigateTo({
        url: '/pages/home/pages/party9/party9',
      })
    } else if (num == 10) { //问卷调查
      wx.navigateTo({
        url: '/pages/home/pages/party10/party10',
      })
    }
  },

  //党务文章和党建发布详情
  datail(e) {
    let num = e.currentTarget.dataset.num
    let id = e.currentTarget.dataset.id
    if (num == 1) {
      wx.navigateTo({
        url: '/pages/home/pages/articleDetail/articleDetail?id=' + id,
      })
    } else if (num == 2) {
      wx.navigateTo({
        url: '/pages/home/pages/blackboardDetail/blackboardDetail?id=' + id,
      })
    }
  }
})